package org.maurange.formation.licpro;

import java.util.ArrayList;
import java.util.List;

import org.maurange.formation.licpro.rest.Arret;
import org.maurange.formation.licpro.rest.ArretDetails;
import org.maurange.formation.licpro.rest.Ligne;
import org.maurange.formation.licpro.rest.NumLigne;

import android.content.Context;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ArretAdapterDetails extends BaseAdapter
{
	
	private Context mContext;
	private List<ArretDetails> mListLignes;
	private LayoutInflater inflater;
	
	public ArretAdapterDetails(Context p_oContext, ArrayList<ArretDetails> arrayList)
	{
		super();
		mContext = p_oContext;
		mListLignes = arrayList;
		this.inflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mListLignes.size();
	}

	@Override
	public ArretDetails getItem(int position) {
		// TODO Auto-generated method stub
		return mListLignes.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int p_iPosition, View p_oConvertView, ViewGroup p_oParentView) {
		if (p_oConvertView == null) {
			p_oConvertView = inflater.inflate(R.layout.details_arret, null);
		}
		else {
			
		}
		ArretDetails mArretDetails= this.getItem(p_iPosition);
//		Log.d(LOG_TAG, "getView() - mArret: " + mArret);
//		Log.d(LOG_TAG, "getView() - mArret: " + mArret.getLibelle());
//		Log.d(LOG_TAG, "getView() - mArret: " + mArret.getDistance());
		
		TextView uiLigneNom = (TextView) p_oConvertView.findViewById(R.id.sens);
		uiLigneNom.setText(mArretDetails.getTerminus());

		TextView uiArretDistance = (TextView) p_oConvertView.findViewById(R.id.temp_attente);
//		Log.d(LOG_TAG, "getView() - uiArretDistance: " + uiArretDistance);
		if ( mArretDetails.getTemps() != null )
			uiArretDistance.setText(mArretDetails.getTemps());
		else
			uiArretDistance.setText("Inconnu");
		
		LinearLayout uiListeArret = (LinearLayout) p_oConvertView.findViewById(R.id.ligne);
		uiListeArret.removeAllViews();
		ImageView uiArretNumLigne;
		int imageSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, mContext.getResources().getDisplayMetrics());
		int imagePadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,2, mContext.getResources().getDisplayMetrics());
		if (mArretDetails.getLigne() != null){
			//Optimisation : recycler les images instanci�es
			uiArretNumLigne=new ImageView(mContext);
			uiArretNumLigne.setImageResource(mArretDetails.getLigne().getNumLigneImageResourceId(mContext));
			uiArretNumLigne.setAdjustViewBounds(true);
			uiArretNumLigne.setMaxHeight(imageSize);
			uiArretNumLigne.setPadding(imagePadding,imagePadding,imagePadding,imagePadding);
			uiListeArret.addView(uiArretNumLigne);
		}

		return p_oConvertView;
	}

	public List<ArretDetails> getListLignes() {
		return mListLignes;
	}
}
